package com.nccsjz.back.resources.web;

import java.io.*;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import org.apache.struts2.ServletActionContext;
import com.nccsjz.back.resources.service.ResourcesService;
import com.nccsjz.base.BaseAction;
import com.nccsjz.pojo.Resources;
import com.nccsjz.utils.Pager;

@SuppressWarnings("serial")
public class ResourcesAction extends BaseAction {
	// 存放url中的参数
	private long id;
	// 存放分页信息
	private Pager pager;
	// 页码
	private int pageNo = 1;
	// 每页多少行
	private int pageSize = 40;
	// 存放多选中的id
	private String[] checkbox;
	private ResourcesService resService;
	// 资源实体类
	private Resources res;
	// 配置上传的相关设置
	private File myFile;
	private String myFilecontentType;
	// 这个名字是固定的格式，以前怎么不是这样的呢？
	private String myFileFileName;
	private String resourceTitle;
	private String title;

	private String dowfileName;// 要下载的文件名

	public String getDowfileName() throws SQLException,
			UnsupportedEncodingException {
		String filename = res.getResourcepath();
		dowfileName = filename
				.substring(filename.lastIndexOf(File.separator) + 1);
		dowfileName = new String(dowfileName.getBytes("GB18030"), "ISO8859-1");
		return dowfileName;
	}

	public void setDowfileName(String dowfileName) {
		this.dowfileName = dowfileName;
	}

	public String getTitle() {
		return title;
	}

	public void setTitle(String title) {
		this.title = title;
	}

	public File getMyFile() {
		return myFile;
	}

	public void setMyFile(File myFile) {
		this.myFile = myFile;
	}

	public String getMyFilecontentType() {
		return myFilecontentType;
	}

	public void setMyFilecontentType(String myFilecontentType) {
		this.myFilecontentType = myFilecontentType;
	}

	public String getMyFileFileName() {
		return myFileFileName;
	}

	public void setMyFileFileName(String myFileFileName) {
		this.myFileFileName = myFileFileName;
	}

	public String getResourceTitle() {
		return resourceTitle;
	}

	public void setResourceTitle(String resourceTitle) {
		this.resourceTitle = resourceTitle;
	}

	public Resources getRes() {
		return res;
	}

	public void setRes(Resources res) {
		this.res = res;
	}

	public long getId() {
		return id;
	}

	public void setId(long id) {
		this.id = id;
	}

	public Pager getPager() {
		return pager;
	}

	public void setPager(Pager pager) {
		this.pager = pager;
	}

	public int getPageNo() {
		return pageNo;
	}

	public void setPageNo(int pageNo) {
		this.pageNo = pageNo;
	}

	public int getPageSize() {
		return pageSize;
	}

	public void setPageSize(int pageSize) {
		this.pageSize = pageSize;
	}

	public String[] getCheckbox() {
		return checkbox;
	}

	public void setCheckbox(String[] checkbox) {
		this.checkbox = checkbox;
	}

	// page display resources
	public String list_Resources() throws Exception {
		resService = new ResourcesService();
		pager = resService.getResources(pageSize, pageNo);
		return SUCCESS;
	}

	// see one resources
	public String view_Resources() throws Exception {
		resService = new ResourcesService();
		res = resService.getResources(id);
		return "view";
	}

	// delete one resources
	public String delete_Resources() throws Exception {
		resService = new ResourcesService();
		boolean flag = resService.deleteResources(id);
		if (flag) {
			return "list";
		} else {
			return "error";
		}
	}

	// batch delete resources
	public String deleteBatch_Resources() throws Exception {
		resService = new ResourcesService();
		List<Long> list = new ArrayList<Long>();
		for (String s : checkbox) {
			list.add(Long.parseLong(s));
		}
		boolean flag = resService.deleteBatchResouces(list);
		if (flag) {
			return "list";
		} else {
			return ERROR;
		}

	}

	// select resource by title
	public String list_ResourcesByTitle() throws Exception {
		resService = new ResourcesService();
		pager = resService.getResources(pageSize, pageNo, title);
		return SUCCESS;
	}

	// upload file
	public String add_Resources() throws Exception {
		int index = myFileFileName.lastIndexOf(".");
		String filename = myFileFileName.substring(0, index);
		String suffix = myFileFileName
				.substring(index, myFileFileName.length());
		String path = ServletActionContext.getServletContext().getRealPath(
				"/uploadFiles");
		String newName = filename + "_" + (new Date()).getTime() + suffix;
		// 检查并创建上传文件路径
		utils.FileUtils.makeDir(path);
		File src = new File(path + File.separator + newName);
		BufferedOutputStream bos = null;
		BufferedInputStream bis = null;
		try {
			FileInputStream fis = new FileInputStream(myFile);
			bis = new BufferedInputStream(fis);
			FileOutputStream fos = new FileOutputStream(src);
			bos = new BufferedOutputStream(fos);
			byte[] buf = new byte[4096];
			int len = -1;
			while ((len = bis.read(buf)) != -1) {
				bos.write(buf, 0, len);
			}
		} finally {
			if (null != bis) {
				bis.close();
			}
			if (null != bos) {
				bos.close();
			}
		}
		Resources re = new Resources();
		re.setResourcetitle(resourceTitle);
		re.setResourcepath(path + File.separator + newName);
		re.setUploaddate(new Date());
		resService = new ResourcesService();
		boolean flag = resService.addResources(re);
		if (flag) {
			return "list";
		} else {
			return ERROR;
		}
	}

	// 前台"下载资源"显示列表
	public String listf_Resources() throws Exception {
		resService = new ResourcesService();
		pager = resService.getResources(pageSize, pageNo);
		return "frontdownres";
	}

	public InputStream getInputStream() throws SQLException,
			FileNotFoundException {
		resService = new ResourcesService();
		res = resService.getResources(id);
		InputStream o = new FileInputStream(res.getResourcepath());
		return o;
	}

	// 前台下载资源给客户。
	public String downRes_Resources() throws Exception {
		return "success";
	}

}
